<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  fan.php
//  phpweb
//  
//  Created by WillLee on 2011-07-06.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Invite extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
 		$this->load->model('common/security_model');
		if($this->security_model->isLogin() === FALSE){
			showmessage('请登录', 'login', 2);
		}
		$this->load->model('user_model');
		$this->load->model('invite_model');
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
	}
	
	public function index()
	{
		$usergroup = $this->config->item('usergroup');
		if ($usergroup[$this->user['groupid']]['invitetime']) {
			if ($this->user['invitedtime'] >= $usergroup[$this->user['groupid']]['invitetime']) {
				showmessage('对不起，您的邀请次数已用尽。', 'index', 2);
			}
		}
		
		$data['self'] = $this->security_model->checkself($this->user['uid']);
		$data['userinfo'] = $this->user;
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($this->uid);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		if (submitcheck('invitesubmit')) {
			$email = $this->input->post('email') ? trim($this->input->post('email', true)) : '';
			if ($email) {
				if ($email == $this->user['username']) {
					showmessage('您不能邀请自己', 'invite/index', 2);
				}
				$code = strtolower(md5(uniqid()));
				$inserdata = array(
								'uid' => $this->user['uid'], 
								'code' => $code, 
								'name' => $this->user['name'], 
								'type' => 1, //邮件邀请类型为1
								'email' => $email,
								'dateline' => now(), 
				);
				$id = $this->invite_model->add($inserdata);

				// 增加邀请数量
				$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET invitedtime = invitedtime + 1 WHERE uid='".$this->user['uid']."'");			
				if ($usergroup[$this->user['groupid']]['invitetime']) {
					$lastinvitetime  = $usergroup[$this->user['groupid']]['invitetime'] - ($this->user['invitedtime'] +1);
					// 减少剩余邀请次数
					$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET invitetime = '".$lastinvitetime."' WHERE uid='".$this->user['uid']."'");
					
				}
				// 更新session
				$this->security_model->update_user_session();
				
				if ($id) {
					//邮件内容
			 		$this->load->model('common/mail_model');
					$data = array();
					$inviteurl = base_url().'register/invite/'.urlencode($inserdata['email']).'/'.$inserdata['code'];
					$data['message'] = "您的朋友".$inserdata['name']."邀请您加入TDAYE社区<br><hr>请点击以下链接完成注册：<br/><a href='".$inviteurl."' target='_blank'>$inviteurl</a>";
					
					$html = $this->load->template('sendmail_tpl', $data, true);
					$maildata = array(
									'mailTo' => $inserdata['email'],
									'mailFrom' => 'hi@tdaye.com',
									'displayName' => 'TDAYE社区',
									'mailSubject' => '您的好友'.$inserdata['name'].'邀请您加入TDAYE社区',
									'mailMessage' => $html
						);
					if ($this->mail_model->sendMail($maildata, 'html')) {
						showmessage('邀请邮件发送成功', 'invite/index', 2);
					}else {
						showmessage('邀请邮件发送失败', 'invite/index', 2);
					}
					showmessage('发送成功', 'invite/index', 2);
				}else{
					showmessage('邀请失败', 'invite/index', 2);
				}
			}else{
				showmessage('请填写邀请邮箱', 'invite/index', 2);
			}
		}

		$this->load->template('invite_tpl', $data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */